/*
 * SimpleAuthorizationDialog.java
 *
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.gui.authorize;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 * Simple dialog winfow that asks login/password pair for authorization
 *
 * @author <a href="mailto:beast.tiberiumlabs@gmail.com">Beast</a>
 */
public class SimpleAuthorizationDialog {
          
    // modal dialog to get user ID and password
    private static final String[] ConnectOptionNames = { "Login", "Cancel" };
    private static final String   ConnectTitle = "FTP requires authorization";
    
    /**
     * shows dialog window and asks user for login/password input
     */
    public AuthorizationBean performLogin() throws AuthorizationCanceled{
        JLabel     userNameLabel = new JLabel("Login:   ", JLabel.RIGHT);
        JTextField userNameField = new JTextField("");
        JLabel     passwordLabel = new JLabel("Password:   ", JLabel.RIGHT);
        JTextField passwordField = new JPasswordField("");
        
        
        JPanel namePanel = new JPanel(false);
        namePanel.setLayout(new BoxLayout(namePanel, BoxLayout.Y_AXIS));
        namePanel.add(userNameLabel);
        namePanel.add(Box.createVerticalStrut(5));
        namePanel.add(passwordLabel);
        namePanel.add(Box.createVerticalStrut(2));
        
        JPanel fieldPanel = new JPanel(false);
        fieldPanel.setLayout(new BoxLayout(fieldPanel, BoxLayout.Y_AXIS));
        fieldPanel.add(userNameField);
        fieldPanel.add(Box.createVerticalStrut(5));
        fieldPanel.add(passwordField);
        fieldPanel.add(Box.createVerticalStrut(2));
        
        JPanel connectionPanel;
        connectionPanel = new JPanel(false);
        connectionPanel.setLayout(new BoxLayout(connectionPanel, BoxLayout.X_AXIS));
        connectionPanel.add(namePanel);
        connectionPanel.add(fieldPanel);
        
        int ret = JOptionPane.showOptionDialog(null, connectionPanel,
                ConnectTitle,
                JOptionPane.OK_CANCEL_OPTION,
                JOptionPane.QUESTION_MESSAGE,
                null, ConnectOptionNames,
                ConnectOptionNames[0]);
        
        if (ret == 1 || ret == JOptionPane.CLOSED_OPTION) {
            throw new AuthorizationCanceled();
        }
        
        AuthorizationBean credentials = new AuthorizationBean();
        credentials.setLogin(userNameField.getText());
        credentials.setPassword(passwordField.getText());
        
        return credentials;
    }
    
}


